package com.example.demo_mvp.base;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

public class BasePresenter<V> {
    //强引用:不会回收 软引用:内存不足 弱引用:垃圾回收 虚引用:没有引用
    WeakReference<V> weakReference;//弱引用
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();//网络请求链接
    //绑定view
    public void attachView(V view){
        weakReference = new WeakReference<>(view);
    }
    //解除绑定
    public void disAttach(){
        if (weakReference != null){
            weakReference.clear();
            weakReference = null;
        }
        if (compositeDisposable != null){
            compositeDisposable.clear();
        }
    }
    //是否绑定view
    public boolean isAttach(){
        return weakReference != null && weakReference.get() != null;
    }
    //返回绑定数据的view
    public V getView(){
        return  weakReference != null && weakReference.get() != null ? weakReference.get() : null;
    }
}
